/**
 * Created with IntelliJ IDEA.
 * Description:认识异常
 * User: 阿宾
 * Date: 2024-08-07
 * Time: 19:43
 */
public class Test01 {
    public static void main(String[] args) {

    }
    public static void main4 (String[] args) {
        try{
            int[] arr = {1,2,3};
            arr[100] = 10;
            arr[0] = 10;
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println(e);
        }finally {
            System.out.println("finally中的代码一定会执行");
        }
        System.out.println("如果没有抛出异常，或者异常被处理了，try-catch-finally后的代码也会执行");
    }
    public static int countSegments(String s){
        s = s.trim();//消除左右空格
        if(s == null || s.isEmpty()){
            return 0;
        }
        int count = 0;//计数器
        String[] str = s.split(" ");//按照空格进行拆分
        for (int i = 0; i < str.length; i++) {
            if(str[i].isEmpty()){
                continue;
            }
            count++;
        }
        return count;
    }
    public static void main3(String[] args) {
        String str = "  hell  world";
        System.out.println(countSegments(str));
    }
    public static void main2(String[] args) {
        int a = 0;
        if(a == 0){
            //抛出一个自定义异常
            throw new ArithmeticException("a == 0");//算术异常
        }
    }
    public static void main1(String[] args) {
        int[] arr = null;
        System.out.println(arr.length);
    }
}
